<?php
	/** sous fonctionnalité de modification de la photo d'un membre
	 * @author Sebastian Mosser
	 */

class sfMemberPictureEdit extends aSubFeature {


	public function __construct() {
		  $this->_acl = new AccessControlList(); 
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(7);
		}


	protected function run() {
		$action = PropRetriever::getProp($_POST,"action");
		switch($action) {
			case "load" :
				return $this->loadNewPic();
			case "del" : 
				return $this->delPic();
			default:
				return $this->displayForm();
		}
	}
	
	private function delPic() {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		// Cas ou il n'y a rien a	 faire
		if ($aMember->getPicture() == Member::$NO_PICTURE)
			return $this->displayForm();
		// CAS OU IL FAUT TRAVAILLER UN PEU
		$oldName = $aMember->getPicture();
		$oldFile = BALLOON_FS_PATH . Member::$PICS_DIR . $oldName;
		if (file_exists($oldFile))
			unlink($oldFile);
		$aMember->setPicture(Member::$NO_PICTURE);
		$aMember->update();
		return $this->displayForm();
	}
	
	private function loadNewPic() {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		
		// Etape 1 : on Dégage l'ancien Fichier
		$oldName = $aMember->getPicture();
		$oldFile = BALLOON_DATA_FS_PATH . Member::$PICS_DIR . $oldName;
		if ($aMember->getPicture() != Member::$NO_PICTURE && file_exists($oldFile))
			unlink($oldFile);
		
		
		$upFile = new UploadedFile("picture");
		$tmpFile = $upFile->getTempName();
		$thePic = new Picture($tmpFile);
		$ext = $thePic->getExtension();
		
		// On retaille
		//$thePic->resize(Member::$PICTURE_SIZE,Member::$PICTURE_SIZE);
		$thePic->resize(Member::$PIC_WIDTH,Member::$PIC_HEIGHT);
		
		// On déplace
		$path = BALLOON_DATA_FS_PATH . Member::$PICS_DIR . $memberId . $ext; 
		$upFile->movetoDest($path);
		
		// On met a jour le membre
		$aMember->setPicture($memberId . $ext);
		$aMember->update();
		
		//return $this->displayForm();
		header("Location: sub_editMember.php?id=".$memberId);
	}
	
	protected function displayForm() {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		$page = "";

	  $page = "<h1> Modification de la photographie </h1>";
	  $page .= "<h2> Attention </h2>";
	  $page .= "<p> Le système Balloon peut uniquement manipuler des images au format suivants, et de taille inférieure a <code> 20 </code> mégas : ";
	  $page .= "<ul> <li> <span class=\"error\"> JPG </span> </li>";
	  $page .= "<li> <span class=\"error\"> GIF </span> </li>";
	  $page .= "<li> <span class=\"error\"> PNG </span> </li> </ul> </p>";
		$maxFileSize = MAX_FILE_SIZE;
		$currentPic = $aMember->getPictureUrl();
		$button = sfMemberEdit::getReturnButton($memberId);
		
		$page .= <<<EOS
<div align="center">
	<fieldset>
		<legend> Modification de la Photographie </legend>
		<br />
		<form method="post" enctype="multipart/form-data" >
		<input type="hidden" name="MAX_FILE_SIZE" value="${maxFileSize}" />
		<label>
		<div align="center">
		<img src="${currentPic}" alt="photo actuelle"/>
		</div>
		</label>
		<br />
		<label> 
			<input type="file" name="picture" class="button" class="edit" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<input type="hidden" name="memberId" value="$memberId" />
		<input type="hidden" name="action" value="load" />
		<input type="submit" value="Charger" class="edit" />
		</form>
		&nbsp;&nbsp;&nbsp;&nbsp;
		
		<form method="post" >
		<input type="hidden" name="memberId" value="$memberId" />
		<input type="hidden" name="action" value="del" />
		<input type="submit" value="Supprimer" class="edit" />
		</form>
		<br /> <br />
		${button}
		<br />  <br />
	</fieldset>
</div>
EOS;

		$page .= "<h2> Explications </h2>";
		$page .= "<p> <ul> <li> Cliquez sur <span class=\"error\">[ Parcourir ]</span> pour selectionner la nouvelle photographie sur votre disque dur, puis cliquez sur <span class=\"error\">[ Charger ]</span> pour l'envoyer dans le système </li>";
		$page .= "<li> Pour supprimer la photo, cliquez sur <span class=\"error\"> [ Supprimer ] </span></li>";
		$page .= "</ul></p>";
		return $page;
	}
	
}

?>